package nuist.hanniko.stu.webbackend.controller.api;

import nuist.hanniko.stu.webbackend.util.ResponseData;
import nuist.hanniko.stu.webbackend.model.User;
import nuist.hanniko.stu.webbackend.service.LoginStateService;
import nuist.hanniko.stu.webbackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserRestController {

    private final UserService userService;

    @Autowired
    public UserRestController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("hello")
    public String hello() {
        return "Hello";
    }

    @PostMapping("register")
    @ResponseBody
    public ResponseData register(@RequestParam(name="id") String id,
                                 @RequestParam(name="password") String password,
                                 @RequestParam(name="name") String name) {
        ResponseData response = new ResponseData();

        if (userService.register(new User(id, password, name)) != userService.SUCCESS) {
            response.setStatus(false);
            response.setMessage("Fail, " + id + " has already been registered.");
        }

        return response;
    }

    @PostMapping("login")
    @ResponseBody
    public ResponseData login(@RequestParam(name="id") String id,
                              @RequestParam(name="password") String password) {

        ResponseData response = new ResponseData();

        Map<String, Object> loginStatus = null;

        if (id != null && password != null) {
            loginStatus = userService.login(id, password);
        }

        if (loginStatus == null) {
            response.setMessage("unknown token or expired token");
            response.setStatus(false);
        } else {
            response.setData(loginStatus);
        }

        return response;
    }

    @PostMapping("modify")
    @ResponseBody
    public ResponseData modify(@RequestParam(name="uid") String id,
                               @RequestParam(name="password") String password,
                               @RequestParam(name="name") String name) {
        ResponseData responseData = new ResponseData();

        User user = new User(id, password, name);
        if (userService.modify(user) != userService.SUCCESS) {
            responseData.setStatus(false);
            responseData.setMessage("some error occurs");
        }
        return responseData;
    }

    @PostMapping("role/add")
    @ResponseBody
    public ResponseData addRole(@RequestParam(name = "uid") String id,
                                @RequestParam(name = "role") String role) {
        ResponseData responseData = new ResponseData();

        if (userService.addRole(id, role) != userService.SUCCESS) {
            responseData.setMessage("role already exists");
            responseData.setStatus(false);
        }
        return responseData;
    }

    @PostMapping("role/del")
    @ResponseBody
    public ResponseData delRole(@RequestParam(name = "uid") String id,
                                @RequestParam(name = "role") String role) {
        ResponseData responseData = new ResponseData();

        if (userService.deleteRole(id, role) != userService.SUCCESS) {
            responseData.setStatus(false);
            responseData.setMessage("role does not exist");
        }
        return responseData;
    }
}
